
/*
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.mrd.gtimpframe.singlewindow.entity;

import com.mrd.util.StringUtils;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.time.DateUtils;
import org.dom4j.Document;
import org.dom4j.Element;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import util.XMLUtils;

/**
 * 客户端回执
 * Created by dengdc on 2017/9/27.
 */
public class SWClientResp {
    /**
     * 处理结果
     * 0 -- 处理失败
     * 1 -- 处理成功
     */
    private String resultFlag;
    /**
     * 错误代码
     */
    private String failCode;
    /**
     * 错误信息
     */
    private String failInfo;
    /**
     * 业务流水号
     */
    private String bizSeqNo;
    /**
     * 业务类型
     */
    private String bizType;
    /**
     * 回执内容
     */
    private List<Element> receipt;

    /**
     * 回执内容
     */
    private String retData;


    private SWClientRespReceipt swClientRespReceipt;

    public List<SWClientRespReceipt> getRetDataSubReceipt() {

        List<Element> elements = receipt;
        List<SWClientRespReceipt> receipts = null;
        if (CollectionUtils.isNotEmpty(elements)) {
            receipts = new ArrayList<>();
            for (Element ele : elements) {
                SWClientRespReceipt receipt = new SWClientRespReceipt();
                receipt.setCusCiqNo(XMLUtils.readText(ele, "cusCiqNo"));
                receipt.setEntryId(XMLUtils.readText(ele, "entryId"));
                receipt.setNoticeDate(XMLUtils.readText(ele, "noticeDate"));
                receipt.setChannel(XMLUtils.readText(ele, "channel"));
                receipt.setNote(XMLUtils.readText(ele, "note"));
                receipt.setCustomMaster(XMLUtils.readText(ele, "customMaster"));
                receipt.setiEDate(XMLUtils.readText(ele, "IEDate"));
                receipt.setdDate(XMLUtils.readText(ele, "dDate"));
                receipts.add(receipt);
            }
            receipts.sort((r1, r2) -> {
                String ds1 = r1.getNoticeDate();
                String ds2 = r2.getNoticeDate();
                String[] pattern = {"yyyy-mm-dd HH:mm:ss"};
                try {
                    Date d1 = DateUtils.parseDate(ds1, pattern);
                    Date d2 = DateUtils.parseDate(ds2, pattern);
                    return Long.valueOf(d1.getTime() - d2.getTime()).intValue();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return 0;
            });
        }
        return receipts;
    }

    public boolean isSuccessResult() {
        return "1".equals(this.resultFlag);
    }

    public String getResultFlag() {
        return resultFlag;
    }

    public void setResultFlag(String resultFlag) {
        this.resultFlag = resultFlag;
    }

    public String getFailCode() {
        return failCode;
    }

    public void setFailCode(String failCode) {
        this.failCode = failCode;
    }

    public String getFailInfo() {
        return failInfo;
    }

    public void setFailInfo(String failInfo) {
        this.failInfo = failInfo;
    }

    public String getBizSeqNo() {
        return bizSeqNo;
    }

    public void setBizSeqNo(String bizSeqNo) {
        this.bizSeqNo = bizSeqNo;
    }

    public String getBizType() {
        return bizType;
    }

    public void setBizType(String bizType) {
        this.bizType = bizType;
    }

    public List<Element> getReceipt() {
        return receipt;
    }

    public void setReceipt(List<Element> receipt) {
        this.receipt = receipt;
    }

    public String getRetData() {
        return retData;
    }

    public void setRetData(String retData) {
        this.retData = retData;
    }

    public SWClientRespReceipt getSwClientRespReceipt() {
        return swClientRespReceipt;
    }

    public void setSwClientRespReceipt(SWClientRespReceipt swClientRespReceipt) {
        this.swClientRespReceipt = swClientRespReceipt;
    }
}
